package com.wzy.ssm.notice.controller;

import com.wzy.ssm.notice.model.NoticeList;
import com.wzy.ssm.notice.model.NoticeListExample;
import com.wzy.ssm.notice.service.NoticeListService;
import com.wzy.ssm.util.IdUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//添加注解
@Controller
public class NoticeListController {

    private NoticeListService noticeListService;

    //通过构�?�器注入service
    public NoticeListController(NoticeListService noticeListService){
        this.noticeListService=noticeListService;
    }

    //查询数据
    @GetMapping("/noticeList")
    @ResponseBody
    public List<NoticeList> selectNoticeList(){
        //1 构�?�查询条�?
        //1.1 创建查询条件�?
        NoticeListExample example = new NoticeListExample();
        //1.2 创建条件构�?�器
        NoticeListExample.Criteria criteria = example.createCriteria();
        //1.3 通过criteria设置条件
        //...

        //2 根据条件查询
        List<NoticeList> list = noticeListService.selectByExample(example);

        return list;
    }
    //添加数据
    @PostMapping("/noticeList")
    @ResponseBody
    public NoticeList insertUser(@RequestBody NoticeList noticeList){
        //准备数据
        //...
        Integer id = IdUtil.nextId9();
        noticeList.setId(id);
        noticeListService.insertSelective(noticeList);

        return noticeList;
    }
    //修改数据
    @PutMapping("/noticeList")
    @ResponseBody
    public NoticeList updateUser(@RequestBody NoticeList noticeList){
        //准备数据
        //...

        noticeListService.updateByPrimaryKeySelective(noticeList);
        //只返回修改的字段数据
        //return noticeList;
        //或�??,通过主键,从数据库查询完整的数�?,然后返回
        return noticeListService.selectByPrimaryKey(String.valueOf(noticeList.getId()));

    }
    //删除数据
    @DeleteMapping("/noticeList/{id}")
    @ResponseBody
    public String delUser(@PathVariable("id") String noticeListId){

        noticeListService.deleteByPrimaryKey(noticeListId);

        //返回状�?�码
        return "200";
    }
}
